//
//  BalloonView.m
//  Balloon
//
//  Created by apple on 16/4/4.
//  Copyright © 2016年 wangxiaofa. All rights reserved.
//

#import "BalloonView.h"
#import "HEBalloonModel.h"

#define SCREEN_WIDTH  [[UIScreen mainScreen]bounds].size.width
#define SCREEN_HEIGHT  [[UIScreen mainScreen]bounds].size.height

#define SMALLEST_SCALE 0.3   //shrink the balloons before move
#define BIGGEST_SCALE 1.3    // amplify the ballons when arrive end

#define MOVING_TIME 1       // the time of balloons moving from origin to end

#define SMALL_SIZE_OF_BALLOON 80         //the size of small balloon
#define BIG_SIZE_OF_BALLOON 160         //the size of big balloon

#define AMPLITUDE 2                 // the amplitude of the balloon
#define SWING_TIME  1               // the duration of swing

@interface BalloonView ()

@property(nonatomic,strong)NSArray *taps;
 
@property (nonatomic, assign) BOOL isFinish;

@property (nonatomic, strong) NSMutableArray *bubbleFrameArr;
@property (nonatomic, strong) NSMutableArray *bubbles;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UILabel *subLb;
@property (nonatomic, strong) NSString *value;

@end

@implementation BalloonView

- (id)initWithFrame:(CGRect)frame balloonTitles:(NSArray*)titles
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _titles = titles;
        [self initSmallData];
        [self setPointForBalloon: titles];
    }

    return self;
}
 
- (void)configTitles:(NSArray *)titles {
    _titles = titles;
    [self initSmallData];
    [self setPointForBalloon: titles];
}

- (void)initSmallData {
    
    _bubbleFrameArr = @[].mutableCopy;
    
    CGFloat topHeight = 180;
    
    HEBalloonModel *center = [HEBalloonModel new];
    center.frame = CGRectMake(-120, SCREEN_HEIGHT-100, BIG_SIZE_OF_BALLOON, BIG_SIZE_OF_BALLOON);
    center.point = CGPointMake(SCREEN_WIDTH/2, topHeight);
    center.swingDirection = 2;

    
    HEBalloonModel *bu1 = [HEBalloonModel new];
    bu1.frame = CGRectMake(SCREEN_WIDTH+100, SCREEN_HEIGHT-100, SMALL_SIZE_OF_BALLOON, SMALL_SIZE_OF_BALLOON);
    bu1.point = CGPointMake(SCREEN_WIDTH/2+100,  topHeight-60);
    bu1.swingDirection = -1.5;
    
    HEBalloonModel *bu2 = [HEBalloonModel new];
    bu2.frame = CGRectMake(SCREEN_WIDTH+100, SCREEN_HEIGHT-100, SMALL_SIZE_OF_BALLOON, SMALL_SIZE_OF_BALLOON);
    bu2.point = CGPointMake(SCREEN_WIDTH/2+90,  topHeight+70);
    bu2.swingDirection = 1;
    
    HEBalloonModel *bu3 = [HEBalloonModel new];
    bu3.frame = CGRectMake(-100, SCREEN_HEIGHT-300, SMALL_SIZE_OF_BALLOON, SMALL_SIZE_OF_BALLOON);
    bu3.point = CGPointMake(SCREEN_WIDTH/2-80,  topHeight+90);
    bu3.swingDirection = -1;
    
    HEBalloonModel *bu4 = [HEBalloonModel new];
    bu4.frame = CGRectMake(-50, SCREEN_HEIGHT-200, SMALL_SIZE_OF_BALLOON, SMALL_SIZE_OF_BALLOON);
    bu4.point = CGPointMake(SCREEN_WIDTH/2-120,  topHeight-50);
    bu4.swingDirection = 1.5;
    
    HEBalloonModel *bu5 = [HEBalloonModel new];
    bu5.frame = CGRectMake(-100 - SMALL_SIZE_OF_BALLOON, SCREEN_HEIGHT-200, SMALL_SIZE_OF_BALLOON, SMALL_SIZE_OF_BALLOON);
    bu5.point = CGPointMake(SCREEN_WIDTH/2-90 + SMALL_SIZE_OF_BALLOON,  topHeight-120);
    bu5.swingDirection = 1.5;
    
    [_bubbleFrameArr addObject:center];

    [_bubbleFrameArr addObject:bu1];
    [_bubbleFrameArr addObject:bu2];

    [_bubbleFrameArr addObject:bu3];
    [_bubbleFrameArr addObject:bu4];

    [_bubbleFrameArr addObject:bu5];
}

//to set the origin point for all balloons ,the origin point has been arranged in advance
-(void)setPointForBalloon:(NSArray *)titles;
{
    NSInteger i = 0;
    _bubbles = @[].mutableCopy;
    [self removeAllSubviews];

    [self initCenterView];

    for (NSString *title in titles) {
        
        if ((i + 1) > (_bubbleFrameArr.count-1)) {
            return;
        }
 
        HEBalloonModel *model = _bubbleFrameArr[i+1];
        
        UIImageView *bubbleView = [[UIImageView alloc] initWithImage: IMAGE_NAMED(@"small")];
        bubbleView.frame = model.frame;
        bubbleView.userInteractionEnabled = YES;
        [self addSubview: bubbleView];
        
        UILabel *titleLb = [UILabel new];
        titleLb.text = title;
        titleLb.textColor = KWhiteColor;
        titleLb.font = DYBlodFont(15);
        titleLb.textAlignment = NSTextAlignmentCenter;
        [bubbleView addSubview:titleLb];
        
        [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bubbleView).offset(5);
            make.left.equalTo(bubbleView).offset(5);
            make.right.equalTo(bubbleView).offset(-5);
            make.bottom.equalTo(bubbleView).offset(-5);
        }];
        
        model.orgCenterPoint = bubbleView.center;
        [_bubbles addObject:bubbleView];
             
        i++;
    }
}

- (void)initCenterView {
    HEBalloonModel *model = _bubbleFrameArr.firstObject;
    
    UIImageView *bubbleView = [[UIImageView alloc] initWithImage: IMAGE_NAMED(@"small")];
    bubbleView.frame = model.frame;
    bubbleView.userInteractionEnabled = YES;
    [self addSubview: bubbleView];
        
    UILabel *titleLb = [UILabel new];
    titleLb.text = @"健康状况";
    titleLb.textColor = KWhiteColor;
    titleLb.font = DYBlodFont(17);
    titleLb.textAlignment = NSTextAlignmentCenter;
    [bubbleView addSubview:titleLb];
    
    UILabel *subLb = [UILabel new];
    subLb.text = [NSString stringWithFormat:@"档案完成度%@%@", [DYTools isBlankString:_value] ? @"0" :_value, @"%"];
    subLb.textColor = KWhiteColor;
    subLb.font = DYBlodFont(15);
    subLb.textAlignment = NSTextAlignmentCenter;
    [bubbleView addSubview:subLb];
    _subLb = subLb;
    
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bubbleView).offset(55);
        make.left.equalTo(bubbleView).offset(5);
        make.right.equalTo(bubbleView).offset(-5);
        make.height.offset([titleLb.text heightForFont:titleLb.font width:[titleLb.text widthForFont:titleLb.font]] + 5);
    }];
    [titleLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    [subLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLb.mas_bottom).offset(5);
        make.left.equalTo(bubbleView).offset(5);
        make.right.equalTo(bubbleView).offset(-5);
    }];
    [subLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

    model.orgCenterPoint = bubbleView.center;
    [_bubbles insertObject:bubbleView atIndex:0];
}

- (void)setValue:(NSString *)value {
    _value = value;
    NSString *unit = @"%";
    _subLb.text = [NSString stringWithFormat:@"档案完成度%@%@", _value, unit];
}

-(void)startBalloonAnimation;
{
    _isFinish = NO;
    for (int i = 0; i < _bubbles.count; i++) {
        HEBalloonModel *model = _bubbleFrameArr[i];
        [self endAnimationForBalloonView:_bubbles[i] withEndPoint:model.point andSwingDirection:model.swingDirection];
    }
}

- (void)recordTimeHander {
    _isFinish = YES;
}

// the move of balloon to endpoint
- (void)endAnimationForBalloonView:(UIImageView*)balloon withEndPoint:(CGPoint)endPoint andSwingDirection:(int)number 
{
    balloon.transform = CGAffineTransformMakeScale(SMALLEST_SCALE, SMALLEST_SCALE);
    
    [UIView animateWithDuration:MOVING_TIME
                     animations:^{
                         balloon.alpha = 1;
                         balloon.transform = CGAffineTransformMakeScale(BIGGEST_SCALE, BIGGEST_SCALE);
                         balloon.center = endPoint;
                         
                     } completion:^(BOOL finished) {
                         
                         [UIView animateWithDuration:1.0
                                          animations:^{
                                              
                                              balloon.transform = CGAffineTransformMakeScale(1, 1);
                                              
                                          } completion:^(BOOL finished) {
                                              
                                              [self swingRoundWithBalloon:balloon andSwingDirection:number];
                                              
                                          }];
                     }];
}

// when selected balloon , the balloons will move to origin point
-(void)hidedWhenSelectedBalloon:(UITapGestureRecognizer*)tap
{
    NSInteger index = [_taps indexOfObject:tap];
    [_delegate balloonViewDidSelected:index];
    KWeakSelf
    [UIView animateWithDuration:MOVING_TIME
                     animations:^{
        for (int i = 0; i < weakSelf.bubbles.count; i++) {
            HEBalloonModel *model = weakSelf.bubbleFrameArr[i];
            UIImageView *buView = weakSelf.bubbles[i];
            buView.alpha = 0;
            buView.center = model.orgCenterPoint;
            buView.transform = CGAffineTransformMakeScale(0.5, 0.5);
        }
     } completion:^(BOOL finished) {
         [self removeFromSuperview];
     }];
}

-(void)backToOriginPoint;
{
    KWeakSelf
    [UIView animateWithDuration:MOVING_TIME
                     animations:^{
                         
        for (int i = 0; i < weakSelf.bubbles.count; i++) {
            HEBalloonModel *model = weakSelf.bubbleFrameArr[i];
            UIImageView *buView = weakSelf.bubbles[i];
            buView.alpha = 0;
            buView.center = model.orgCenterPoint;
            buView.transform = CGAffineTransformMakeScale(0.5, 0.5);
        }

    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    [self stopAnimation];
}

- (void)stopAnimation {
    _isFinish = YES;
}

- (void)startSwing {
    _isFinish = NO;
    for (int i = 0; i < _bubbles.count; i++) {
        HEBalloonModel *model = _bubbleFrameArr[i];
        UIImageView *balloon = _bubbles[i];
        [self swingRoundWithBalloon:balloon andSwingDirection:model.swingDirection];
    }
}

- (void)stopSwing {
    _isFinish = YES;
}

// swing the balloons
-(void)swingRoundWithBalloon:(UIImageView*)balloon andSwingDirection:(int)number
{
    if (_isFinish) {
        return;
    }
    [UIView animateWithDuration:SWING_TIME
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction|
     UIViewAnimationOptionCurveLinear
                     animations:^{
                         
                         balloon.center = CGPointMake(balloon.center.x-number, balloon.center.y+number);
                         
                     } completion:^(BOOL finished) {
                         [UIView animateWithDuration:SWING_TIME
                                               delay:0
                                             options:UIViewAnimationOptionAllowUserInteraction|
                          UIViewAnimationOptionCurveLinear
                                          animations:^{
                                              
                                              balloon.center = CGPointMake(balloon.center.x+number, balloon.center.y+number);
                                              
                                              
                                          } completion:^(BOOL finished) {
                                              [UIView animateWithDuration:SWING_TIME
                                                                    delay:0
                                                                  options:UIViewAnimationOptionAllowUserInteraction|
                                               UIViewAnimationOptionCurveLinear
                                                               animations:^{
                                                                   
                                                                   balloon.center = CGPointMake(balloon.center.x+number, balloon.center.y-number);
                                                                   
                                                               } completion:^(BOOL finished) {
                                                                   
                                                                   [UIView animateWithDuration:SWING_TIME
                                                                                         delay:0
                                                                                       options:UIViewAnimationOptionAllowUserInteraction|
                                                                    UIViewAnimationOptionCurveLinear
                                                                                    animations:^{
                                                                                        
                                                                                        balloon.center = CGPointMake(balloon.center.x-number, balloon.center.y-number);
                                                                                        
                                                                                    } completion:^(BOOL finished) {
                                                                                        
                                                                                        [self swingRoundWithBalloon:balloon andSwingDirection:number];
                                                                                        
                                                                                    }];
                                                               }];
                                          }];
                     }];
}

@end
